package com.yk.silence.rxmvp.net.file;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.yk.silence.rxmvp.base.BaseObserver;
import com.yk.silence.rxmvp.base.BasePresenter;
import com.yk.silence.rxmvp.net.api.APIServer;
import com.yk.silence.rxmvp.net.retrofit.RetrofitUtils;
import com.yk.silence.rxmvp.util.FileUtils;

import java.io.File;
import java.io.IOException;


import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;

public class FilePresenter extends BasePresenter<DownLoadFileView> {


    public FilePresenter(DownLoadFileView baseView) {
        super(baseView);
    }

    /**
     * 上传文件
     *
     * @param picPath
     * @param phone
     * @param password
     */
    public void upLoadFile(String picPath, String phone, String password) {
        File file = new File(picPath);
        //图片参数
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("uploadFile", file.getName(), requestFile);
        //手机参数
        RequestBody phoneBody = RequestBody.create(MediaType.parse("multipart/form-data"), phone);
        //  密码参数
        RequestBody pswBody = RequestBody.create(MediaType.parse("multipart/form-data"), password);
        addDisposable(RetrofitUtils.getInstance().getApiServer().register(phoneBody, pswBody, body),
                new BaseObserver(getBaseView()) {
                    @Override
                    public void onSuccess(Object o) {

                    }

                    @Override
                    public void onError(String msg) {

                    }
                });
    }

    /**
     * 下载文件
     */
    public void downLoadFile(String url, final String path) {
        OkHttpClient client = new OkHttpClient.Builder()
                .addNetworkInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Response response = chain.proceed(chain.request());
                        return response.newBuilder().body(new ProgressResponseBody(response.body(),
                                new ProgressResponseBody.ProgressListener() {
                                    @Override
                                    public void onProgress(long totalSize, long downSize) {
                                        mView.onProgress(totalSize, downSize);
                                    }
                                })).build();
                    }
                }).build();

        Retrofit retrofit = new Retrofit.Builder().client(client)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl("https://wawa-api.vchangyi.com/").build();

        apiServer = retrofit.create(APIServer.class);

        apiServer
                .downloadFile(url)
                .map(new Function<ResponseBody, String>() {
                    @Override
                    public String apply(ResponseBody body) throws Exception {
                        File file = FileUtils.saveFile(path, body);
                        return file.getPath();
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new FileObserver(mView) {
                    @Override
                    public void onSuccess(File file) {
                        mView.onSuccess(file);
                    }

                    @Override
                    public void onFailed(String msg) {
                        mView.onFailed(msg);
                    }
                });
    }
}
